package com.stx.openeyes.view.fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaeger.library.StatusBarUtil;
import com.stx.openeyes.MainActivity;
import com.stx.openeyes.R;
import com.stx.openeyes.SealConst;
import com.stx.openeyes.dto.Response;
import com.stx.openeyes.model.FindMoreEntity;
import com.stx.openeyes.model.User;
import com.stx.openeyes.utils.CommonAdapter;
import com.stx.openeyes.utils.ContextUtil;
import com.stx.openeyes.utils.HttpAdress;
import com.stx.openeyes.utils.JsonParseUtils;
import com.stx.openeyes.utils.NToast;
import com.stx.openeyes.utils.ViewHolder;
import com.stx.openeyes.view.activity.AboutActivity;
import com.stx.openeyes.view.activity.FunctionActivity;
import com.stx.openeyes.view.activity.HelpActivity;
import com.stx.openeyes.view.activity.LoginActivity;
import com.stx.openeyes.view.activity.MyVideoListActivity;
import com.stx.openeyes.view.activity.UpdatePasswordActivity;
import com.stx.openeyes.widget.LoadDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 我的
 */
public class MyFragment extends Fragment {

    private static final String TAG = "MyFragment";
    protected Context mContext;
    private Gson mGson;
    Intent intent, intent2, intent3;
    private View mview;
    private boolean checkLoginResult = false;

    public MyFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mview = inflater.inflate(R.layout.activity_function, container, false);
        ButterKnife.bind(this, mview);
        mContext = getActivity();
        intent = new Intent(mContext, MyVideoListActivity.class);
        intent2 = new Intent(mContext, AboutActivity.class);
        intent3 = new Intent(mContext, HelpActivity.class);
        // 检查登录
        checkLogin();
        return mview;
    }

    @SuppressLint("LongLogTag")
    private void checkLogin() {
        checkLoginResult = false;
        LoadDialog.show(mContext);

        // 拉取用户信息
        OkHttpUtils.get()
                .url(HttpAdress.USER_INFO)
                .addHeader("token", ContextUtil.getCurrentUserToken())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("===userInfo error", e.getMessage());
                        NToast.shortToast(mContext, SealConst.NETWORK_ERROR);
                        LoadDialog.dismiss(mContext);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("===userInfo response json", response);
                        mGson = new Gson();
                        Response<User> userInfoResp = mGson.fromJson(response, new TypeToken<Response<User>>() {}.getType());
                        if (!userInfoResp.isSuccess()) {
                            NToast.shortToast(mContext, userInfoResp.getMessage());
                            restView();
                            LoadDialog.dismiss(mContext);
                            return;
                        }
                        User userInfo = userInfoResp.getData();
                        TextView textView = (TextView) mview.findViewById(R.id.user_mobile);
                        textView.setText(userInfo.getNickname());
                        textView.setClickable(false);
                        //显示修改密码
                        LinearLayout updatePassword = (LinearLayout) mview.findViewById(R.id.xiugaimima);
                        updatePassword.setVisibility(View.VISIBLE);
                        // 显示退出登录
                        LinearLayout logoutTv = (LinearLayout) mview.findViewById(R.id.logoutBtn);
                        logoutTv.setVisibility(View.VISIBLE);
                        // 修改头像
                        ImageView image = (ImageView) mview.findViewById(R.id.user_icon);
                        image.setImageResource(R.mipmap.vatart);
                        LoadDialog.dismiss(mContext);

                        checkLoginResult = true;
                    }
                });
    }

    @OnClick({R.id.user_mobile, R.id.logoutBtn, R.id.my_recommend, R.id.my_buy, R.id.my_see_history, R.id.xiugaimima,R.id.my_download, R.id.my_wallet, R.id.help, R.id.about_us})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_mobile:
                startActivity(new Intent(mContext, LoginActivity.class));
                break;
            case R.id.xiugaimima:
                startActivity(new Intent(mContext, UpdatePasswordActivity.class));
                break;
            case R.id.logoutBtn:
                // TODO: 调用退出登录接口

                SharedPreferences settings = mContext.getSharedPreferences(SealConst.USER_INFO, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();
                editor.clear();
                editor.commit();
                LoadDialog.show(mContext);
                TextView textView = (TextView) this.mview.findViewById(R.id.user_mobile);
                textView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(R.string.click_login_text);
                        textView.setClickable(true);
                        ImageView image = (ImageView) mview.findViewById(R.id.user_icon);
                        image.setImageResource(R.mipmap.tab_me);
                        LinearLayout logoutTv = (LinearLayout) mview.findViewById(R.id.logoutBtn);
                        logoutTv.setVisibility(View.INVISIBLE);
                        LoadDialog.dismiss(mContext);
                        checkLoginResult = false;
                        startActivity(new Intent(mContext, MainActivity.class));
                    }
                }, 1000);
                break;
            case R.id.my_recommend:
                if (checkLoginResult) {
                    intent.putExtra("title", "我的收藏");
                    intent.putExtra("type", 2);
                    startActivity(intent);
                } else {
                    NToast.shortToast(mContext, SealConst.PLEASE_LOGIN);
                }
                break;
            case R.id.my_buy:
                if (checkLoginResult) {
                    intent.putExtra("title", "我的购买");
                    intent.putExtra("type", 3);
                    startActivity(intent);
                } else {
                    NToast.shortToast(mContext, SealConst.PLEASE_LOGIN);
                }
                break;
            case R.id.my_see_history:
                if (checkLoginResult) {
                    intent.putExtra("title", "观看记录");
                    intent.putExtra("type", 1);
                    startActivity(intent);
                } else {
                    NToast.shortToast(mContext, SealConst.PLEASE_LOGIN);
                }
                break;
            case R.id.my_download:
                if (checkLoginResult) {
                    intent.putExtra("title", "我的下载");
                    intent.putExtra("type", 4);
                    startActivity(intent);
                } else {
                    NToast.shortToast(mContext, SealConst.PLEASE_LOGIN);
                }
                break;
            case R.id.my_wallet:
                if (checkLoginResult) {
                    intent.putExtra("title", "我的钱包");
                    intent.putExtra("type", 5);
                    startActivity(intent);
                } else {
                    NToast.shortToast(mContext, SealConst.PLEASE_LOGIN);
                }
                break;
            case R.id.help:
                if (checkLoginResult) {
                    intent.putExtra("title", "帮助反馈");
                    startActivity(intent3);
                } else {
                    NToast.shortToast(mContext, SealConst.PLEASE_LOGIN);
                }
                break;
            case R.id.about_us:
                    intent2.putExtra("title", "关于我们");
                    startActivity(intent2);
                break;
        }
    }

    private void restView() {
        SharedPreferences settings = mContext.getSharedPreferences(SealConst.USER_INFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();

        TextView textView = (TextView) mview.findViewById(R.id.user_mobile);
        textView.setText(R.string.click_login_text);
        textView.setClickable(true);
        ImageView image = (ImageView) mview.findViewById(R.id.user_icon);
        image.setImageResource(R.mipmap.tab_me);
        LinearLayout logoutTv = (LinearLayout) mview.findViewById(R.id.logoutBtn);
        logoutTv.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
